package com.example.listener;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class SpringRabbitListener {

//    @RabbitListener(queues = "object.queue1")
//    public void lis(Object o){
//        System.out.println(o);
//    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg) throws InterruptedException {
        System.out.println("消费者1："+msg);
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2："+msg);
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenSimpleQueue3(String msg) throws InterruptedException {
        System.err.println("fanoutQueue1："+msg);

    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenSimpleQueue4(String msg) throws InterruptedException {
        System.err.println("fanoutQueue2："+msg);

    }


    @RabbitListener(bindings = @QueueBinding(
           value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"boy","girl"}
    ))
    public void directQueue1(String msg) throws InterruptedException {
        System.err.println("directQueue1："+msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"boy","gay"}
    ))
    public void directQueue2(String msg) throws InterruptedException {
        System.err.println("directQueue2："+msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void topicQueue1(String msg) throws InterruptedException {
        System.err.println("topicQueue1："+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = {"japen.#","#.new"}
    ))
    public void topicQueue2(String msg) throws InterruptedException {
        System.err.println("topicQueue2："+msg);
    }
}
